<?php
include dirname(__FILE__)."/config/config.php";

// click on banner
if (isset($_GET['url']) && isset($_GET['id'])) {
    $_id = $_GET['id'];
    $_url = $_GET['url'];
    $_banner = mysql_query("SELECT * from ads WHERE id=".$_id);
    $_banner = mysql_fetch_array($_banner);
    $_qry = "UPDATE `ads` SET `clicks` = '" . ($_banner['clicks'] + 1) . "' WHERE `id` ='" . $_banner['id'] . "'";
    mysql_query($_qry);
    
    header('location: '.$_url);
}
    $_borders = (isset($_borders))?$_borders:true;
    $_position = '';//(isset($_position))?"AND position='$_position'":'';
    // banner get and update
    $_banner = mysql_query("SELECT * from ads WHERE banner_url IS NOT NULL AND status=1 AND `assigned` != 0 $_position ORDER BY updated_at LIMIT 1");
    
    if (!mysql_num_rows($_banner)) {
        $_banner = mysql_query("SELECT * from ads WHERE banner_url IS NOT NULL AND status=1 AND `assigned` = 0 ORDER BY updated_at LIMIT 1");
    }
    
    $_banner = mysql_fetch_array($_banner);
    
    
    
    $_setInactive = '';
    if(($_banner['current'] + 1) == $_banner['assigned']){
        $_setInactive = ', `status`=2 '; 
    }

    $_qry = "UPDATE `ads` SET `updated_at`= now() $_setInactive , `current` = '" . ($_banner['current'] + 1) . "' WHERE `id` ='" . $_banner['id'] . "'";
    mysql_query($_qry);

    
?>
<?php
$_path = file_exists('./_banner.php')?'':'../';
?>
<?php if($_borders): ?>
<img src="<?= $_path ?>images/lineDivider.png" style="margin-bottom: 15px;">
<?php endif; ?>
<a href="<?= $_path ?>_banner.php?url=<?= $_banner['url'] ?>&id=<?= $_banner['id'] ?>" target="_blank">
    <img src="<?= $_banner['banner_url'] ?>" />
</a>
<?php if($_borders): ?>
<img src="<?= $_path ?>images/lineDivider.png" style="margin-top: 15px;">
<?php endif; ?>